home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PACKET / CBBS60SO.ZIP / MBFILE.C < prev    next >
Text File  |  1989-01-20  |  11KB  |  545 lines

  1.  
  2. /*
  3.  *  MBFILE.C - 1/20/89
  4.  */
  5.  
  6. #include "mb.h"
  7.  
  8. word filesize;
  9. char *fm;
  10. DIRPATH *dphd;
  11.  
  12. /*
  13.  *  Prepend path given by opt2 to file spec given by p.
  14.  */
  15.  
  16. DIRPATH *getdir(p)
  17. char *p;
  18. {
  19.   register DIRPATH *dp;
  20.  
  21.   for (dp = dphd; dp isnt NULL; dp = dp->next) if (dp->id is port->opt2)
  22.   {
  23.     strcpy(port->line, dp->path);
  24.     strcat(port->line, p);
  25.     if (!(port->mode & p_sysop)) for (; *p; p++)
  26.       if ((*p is ':') or (*p is '\\') or (*p is '/'))
  27.       { port->msg = mcant; return NULL; }
  28.     return dp;
  29.   }
  30.   port->msg = mndir;
  31.   return NULL;
  32. }
  33.  
  34. /*
  35.  *  Display the directory paths.
  36.  */
  37.  
  38. shpaths()
  39. {
  40.   register DIRPATH *dp;
  41.   int crcount = 1;
  42.  
  43.   sprintf(port->line, "Use %c and directory ID:\n", port->opt1);
  44.   outstr(port->line);
  45.   for (dp = dphd; dp isnt NULL; dp = dp->next)
  46.   {
  47.     ++crcount;
  48.     sprintf(port->line, "%c%c %-34s", port->opt1, dp->id, dp->name);
  49.     outstr(port->line);
  50.     if (crcount % 2) outstr("\n");
  51.   }
  52.   outstr("\n");
  53. }
  54.  
  55. /*
  56.  *  Display directory.
  57.  */
  58.  
  59. prtdir()
  60. {
  61.   DIRDEF dirdef;
  62.   DIRENT *dp;
  63.  
  64.   register short gap, i, k, l, ndir;
  65.   register word totsize;
  66.  
  67.   if (port->mode & ops)
  68.   {
  69.     if (port->opt2 is ' ')
  70.     {
  71.       if (port->flds is 1) { shpaths(); return; }
  72.       strcpy(port->line, port->fld[1]);
  73.     }
  74.     else if (getdir(port->fld[1]) is NULL) return;
  75.   }
  76.   else
  77.   {
  78.     if (port->opt2 is ' ') { shpaths(); return; }
  79.     if (getdir(port->fld[1]) is NULL) return;
  80.   }
  81.  
  82.   ndir = 0;
  83.   dp = (DIRENT *)tmp->scr;
  84.  
  85. /*
  86.  *  Get first directory entry.
  87.  */
  88.  
  89.   if (!diropen(port->line, dp, &dirdef)) { port->msg = mfind; return; }
  90.  
  91. /*
  92.  *  Get the rest of the directory entries.
  93.  */
  94.  
  95.   while ((ndir < dirmax) and (dp->size >= 0))
  96.   {
  97.     ndir++;
  98.     dirnext(++dp, &dirdef);
  99.   }
  100.  
  101. /*
  102.  *  Sort 'em.
  103.  */
  104.  
  105.   sort(tmp->scr, ndir, sizeof(DIRENT), tmp->scr + (sizeof(DIRENT) * ndir));
  106.  
  107. /*
  108.  *  Print 'em.
  109.  */
  110.  
  111.   pgst(NULL);
  112.   outchar('\n');
  113.   totsize = 0;
  114.   gap = (ndir + 2) / 3;
  115.   for (i = 0; i < gap; i++)
  116.   {
  117.     if (pgck() is 'Q') break;
  118.     for (l = 0, k = i; (l < 4) and (k < ndir); l++, k += gap)
  119.     {
  120.       totsize += tmp->dirent[k].size;
  121.       if (k isnt i) outstr(" | ");
  122.       sprintf (port->line,
  123.         "%-12s %4uk", tmp->dirent[k].name, tmp->dirent[k].size);
  124.       outstr(port->line);
  125.     }
  126.     outchar('\n');
  127.   }
  128.  
  129.   sprintf(port->line, "\n%uk of %uk used, %uk free.\n\n",
  130.     totsize, dirdef.size, dirdef.free);
  131.   outstr(port->line);
  132. }
  133.  
  134. /*
  135.  *  Output a line from the help file.
  136.  */
  137.  
  138. helpo()
  139. {
  140.   if ((*port->line isnt '!') or (port->user->options & u_sysop))
  141.   { if (*port->line is '!') *port->line = ' '; outstr(port->line); }
  142. }
  143.  
  144. /*
  145.  *  Find specific subject header in file.
  146.  */
  147.  
  148. helps()
  149. {
  150.   register short found = false;
  151.  
  152.   while (!found and (fgets(port->line, linelen, port->fl) isnt NULL))
  153.     if ((port->line[0] is port->opt1) and
  154.         (port->line[2] is port->opt2)) found = true;
  155.  
  156.   if (!found)
  157.   {
  158.     sprintf(port->line, "\nNo help for - %c\n\n", port->opt2);
  159.     port->msg = port->line;
  160.     return;
  161.   }
  162.  
  163.   found = false;
  164.   while (!found and (fgets(port->line, linelen, port->fl) isnt NULL))
  165.   {
  166.     found = ((*port->line is '#') or (*port->line is '?'));
  167.     if (!found) helpo();
  168.   }
  169. }
  170.  
  171. /*
  172.  *  Dump all the help information.
  173.  */
  174.  
  175. helpa()
  176. {
  177.   register short ok = false;
  178.  
  179.   while ((fgets(port->line, linelen, port->fl) isnt NULL))
  180.   {
  181.     if ((*port->line is '#') or (*port->line is '?'))
  182.     {
  183.       ok = (*port->line is port->opt1);
  184.     }
  185.     else if (ok) helpo();
  186.   }
  187. }
  188.  
  189. /*
  190.  *  User wants help.
  191.  */
  192.  
  193. help()
  194. {
  195.   if (port->flds is 1) port->opt2 = '_'; else port->opt2 = *port->fld[1];
  196.   if (port->opt1 is 'H') port->opt1 = '#';
  197.  
  198.   if ((port->fl = fopen(helpfile, "r")) is NULL) { port->msg = mfind; return; }
  199.  
  200.   if (port->opt2 is '?') helpa(); else helps();
  201.  
  202.   fclose(port->fl);
  203. }
  204.  
  205. /*
  206.  *  N command, rename a file.
  207.  */
  208.  
  209. renfil()
  210. {
  211.   register int fl;
  212.  
  213.   if ((fl = open(port->fld[1], O_RDONLY)) < 0)
  214.     { nofile(port->fld[1]); return; }
  215.  
  216.   close(fl);
  217.  
  218.   if ((fl = open(port->fld[2], O_RDONLY)) >= 0)
  219.     { close(fl); port->msg = mexst; }
  220.   else rename (port->fld[1], port->fld[2]);
  221. }
  222.  
  223. /*
  224.  *  Z command, delete a file.
  225.  */
  226.  
  227. kilfil()
  228. {
  229.   if (port->opt2 is ' ') strcpy(port->line, port->fld[1]);
  230.   else if (getdir(port->fld[1]) is NULL) return;
  231.  
  232.   if (!unlink(port->line)) port->msg = mdone; else nofile(port->line);
  233. }
  234.  
  235. /*
  236.  *  Are the two files in the same directory?
  237.  */
  238.  
  239. samedir(t, f)
  240. char *t, *f;
  241. {
  242.   register char *te, *fe;
  243.  
  244. /*
  245.  *  Are they on the same device?
  246.  */
  247.  
  248.   te = strchr(t, ':');
  249.   fe = strchr(f, ':');
  250.   if ((te isnt NULL) or (fe isnt NULL))
  251.   {
  252.     if ((te - t) isnt (fe - f)) return false;
  253.     if (!matchn(te, fe, (int)(fe - f) + 1)) return false;
  254.   }
  255.  
  256. /*
  257.  *  Are they in the same subdirectory on the device?
  258.  */
  259.  
  260.   te = strrchr(t, '\\');
  261.   fe = strrchr(f, '\\');
  262.   if ((te is NULL) and (fe is NULL)) return true;
  263.   if ((te - t) isnt (fe - f)) return false;
  264.   return matchn(t, f, (int)(fe - f) + 1);
  265. }
  266.  
  267. /*
  268.  *  Copy a file.
  269.  */
  270.  
  271. copy(f, t, h)
  272. char *f, *t;
  273. int h;
  274. {
  275.   register int n, in, out;
  276.  
  277.   filesize = 0;
  278.  
  279.   if ((in = open(f, O_RDONLY | O_BINARY)) < 0) return false;
  280.  
  281.   out = open(t, O_CREAT | O_RDWR | O_BINARY, pmode);
  282.   if (h) lseek(out, (long)RECSIZE, 0);
  283.  
  284.   while ((n = read(in, tmp->scr, scrmax)) > 0)
  285.   { filesize += n; write (out, tmp->scr, n); }
  286.  
  287.   close(in);
  288.   close(out);
  289.   return true;
  290. }
  291.  
  292. /*
  293.  *  V command: copy a file.
  294.  */
  295.  
  296. copfil()
  297. {
  298.   register int n;
  299.  
  300.   if ((n = open(port->fld[2], O_RDONLY | O_BINARY)) >= 0)
  301.   { port->msg = mexst; close(n); return; }
  302.  
  303.   if ((n = open(port->fld[1], O_RDONLY | O_BINARY)) < 0)
  304.     { nofile(port->fld[1]); return;}
  305.  
  306.   close(n);
  307.   copy(port->fld[1], port->fld[2], false);
  308. }
  309.  
  310. /*
  311.  *  Upload, common code.
  312.  */
  313.  
  314. uload(tname)
  315. char *tname;
  316. {
  317.   register char *tp;
  318.   register PORTS *p;
  319.  
  320.   p = port;
  321.  
  322.   if ((p->fl = fopen(tname, "w")) is NULL) { p->msg = mcant; return; }
  323.  
  324.   filesize = 0;
  325.   while (true)
  326.   {
  327.     while(!getdat());
  328.  
  329. /*
  330.  *  If user disconnected, timed out, or forced off, zap the file.
  331.  */
  332.  
  333.     if (p->mode & gone)
  334.     {
  335.       fclose(p->fl);
  336.       unlink(tname);
  337.       return;
  338.     }
  339.  
  340.     if ((tp = strchr(p->line, cpmeof)) is NULL)
  341.     {
  342.       filesize += strlen(p->line);
  343.       fputs(p->line, p->fl);
  344.     }
  345.     else
  346.     {
  347.       if (tp isnt p->line)
  348.       {
  349.         *tp++ = '\n';
  350.         *tp   = '\0';
  351.         filesize += strlen(p->line);
  352.         fputs(p->line, p->fl);
  353.       }
  354.       fclose(p->fl);
  355.       return;
  356.     }
  357.   }
  358. }
  359.  
  360. /*
  361.  *  Upload a file, from local console and remote sysop.
  362.  */
  363.  
  364. uloadl()
  365. {
  366.   if ((port->fl = fopen(port->fld[1], "r")) isnt NULL)
  367.     { port->msg = mexst; fclose(port->fl); return; }
  368.   prtx(fm);
  369.   uload(port->fld[1]);
  370. }
  371.  
  372. /*
  373.  *  Upload a file, from logged in user.
  374.  */
  375.  
  376. uloadr()
  377. {
  378.   register DIRPATH *dp;
  379.  
  380.   if (port->opt2 is ' ') { shpaths(); return; }
  381.   if ((dp = getdir(port->fld[1])) is NULL) return;
  382.   if (!(dp->flags & dp_upload)) { port->msg = mcant; return; }
  383.   if (!(port->priv & p_upload)) { port->msg = mcant; return; }
  384.   if ((port->fl = fopen(port->line, "r")) isnt NULL)
  385.     { port->msg = mexst; fclose(port->fl); return; }
  386.   log('F', 'U', ' ', port->line);
  387.   prtx(fm);
  388.   uload(port->line);
  389. }
  390.  
  391. /*
  392.  *  Download, common code.
  393.  */
  394.  
  395. dload(fname)
  396. char *fname;
  397. {
  398.   if ((port->fl = fopen(fname, "r")) is NULL) { nofile(fname); return; }
  399.  
  400.   while(fgets(tmp->scr, scrmax, port->fl) isnt NULL)
  401.   {
  402.      if (chkdis()) break;
  403.      outstr(tmp->scr);
  404.   }
  405.   fclose (port->fl);
  406. }
  407.  
  408. /*
  409.  *  Display the "info" file.
  410.  */
  411.  
  412. dloadi()
  413. {
  414.   dload(infofile);
  415. }
  416.  
  417.  
  418. /*
  419.  *  Download a file, by logged in user.
  420.  */
  421.  
  422. dloadr()
  423. {
  424.   register DIRPATH *dp;
  425.  
  426.   if (port->opt2 is ' ') { shpaths(); return; }
  427.   if ((dp = getdir(port->fld[1])) is NULL) return;
  428.   if (!(dp->flags & dp_dnload)) { port->msg = mcant; return; }
  429.   if (!(port->priv & p_dnload)) { port->msg = mcant; return; }
  430.   log('F', 'D', ' ', port->line);
  431.   dload(port->line);
  432. }
  433.  
  434. /*
  435.  *  Download a file, by remote sysop.
  436.  */
  437.  
  438. dloads()
  439. {
  440.   if (port->opt2 is ' ')
  441.   {
  442.     if (port->flds is 1) { shpaths(); return; }
  443.     strcpy(port->line, port->fld[1]);
  444.   }
  445.   else if (getdir(port->fld[1]) is NULL) return;
  446.  
  447.   log('F', 'D', ' ', port->line);
  448.   dload(port->line);
  449. }
  450.  
  451. /*
  452.  *  Download a file, by local console.
  453.  */
  454.  
  455. dloadl()
  456. {
  457.   register PORTS *tp;
  458.  
  459.   if (port->opt2 is ' ') port->opt2 = 'L';
  460.   if ((tp = findport(port->opt2)) is NULL) { port->msg = mnport; return; }
  461.   if ((cport->fl = fopen(cport->fld[1], "r")) is NULL)
  462.     { nofile(cport->fld[1]); return; }
  463.  
  464.   ioport(tp);
  465.   pgst(NULL);
  466.   while(fgets(tmp->scr, scrmax, cport->fl) isnt NULL)
  467.   {
  468.     outstr(tmp->scr);
  469.     if (pgck() is 'Q') break;
  470.   }
  471.   fclose (cport->fl);
  472.   if (tp isnt cport) term(tp);
  473.   ioport(cport);
  474. }
  475.  
  476. /*
  477.  *  Move killed mail over to a subdirectory if it exists. NTS for all
  478.  *  type 'T' and 'S' mail. Sysop's call for all to and from the sysop
  479.  *  and KILL for non-bbs mail.
  480.  */
  481.  
  482. arcmsg()
  483. {
  484.   register int n, in, out;
  485.   static char bbs[8];
  486.   FILE *index;
  487.  
  488.   msgfile(port->line, port->mmhs->number);
  489.   strcpy(bbs, "KILL");
  490.  
  491.   if (*port->mmhs->bbs isnt ' ')
  492.     unbl(bbs, port->mmhs->bbs, ln_call);
  493.   if ((matchn(port->mmhs->to, cport->user->call, ln_call)) or
  494.     (matchn(port->mmhs->from, cport->user->call, ln_call)))
  495.     unbl(bbs, cport->user->call, ln_call);
  496.   if ((port->mmhs->type is 'T') or (port->mmhs->type is 'S'))
  497.     strcpy(bbs, "NTS");
  498.   if (port->mmhs->stat & m_busy) strcpy( bbs, "BUSY");
  499.  
  500.   while (true)
  501.   {
  502.     sprintf(port->cmd, "%s%s\\%u", msgdir, bbs, port->mmhs->number);
  503.  
  504.     if ((out = open(port->cmd, O_CREAT | O_RDWR | O_BINARY, pmode)) < 0)
  505.     {
  506.       if((matchn(bbs, "KILL", 4)) or (matchn(bbs, "BUSY", 4)))
  507.       {
  508.          unlink(port->line);
  509.          outstr(" Deleted\n");
  510.          return;
  511.       }
  512.       strcpy(bbs, "KILL");
  513.     }
  514.     else break;
  515.   }
  516.   makehdr();
  517.   sprintf(port->cmd, "%s%s\\INDEX", msgdir, bbs);
  518.   if ((index = fopen(port->cmd, "a+")) isnt NULL)
  519.   {
  520.     fprintf( index, "%s", tmp->scr);
  521.     fclose(index);
  522.   }
  523.   remnl (tmp->scr);
  524.   strcat (tmp->scr, "\r\n");
  525.  
  526.   if (*port->mmhs->bid isnt ' ') {
  527.     sprintf(port->cmd, "   BID: -%12.12s\r\n", port->mmhs->bid);
  528.     strcat(tmp->scr, port->cmd);
  529.     }
  530.   write(out, tmp->scr, strlen(tmp->scr));
  531.  
  532.   in = open(port->line, O_RDONLY | O_BINARY);
  533.   lseek(in, (long)RECSIZE, 0);
  534.  
  535.   while ((n = read(in, tmp->scr, scrmax)) > 0)
  536.     write( out, tmp->scr, n);
  537.  
  538.   close(out);
  539.   close(in);
  540.   unlink(port->line);
  541.   sprintf(port->line," Copied to %s\n", bbs);
  542.   outstr(port->line);
  543. }
  544.  
  545.